package com.health.pay.tencent;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.log4j.Logger;
import org.jdom2.JDOMException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.health.pay.tencent.common.MD5;
import com.health.pay.tencent.common.TenpayUtil;
import com.health.pay.tencent.common.XMLUtil;

public class ResponseHandler {

	private static final Logger logger = Logger
			.getLogger(ResponseHandler.class);
	private String key;
	private SortedMap parameters;
	private String debugInfo;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String uriEncoding;
	public ResponseHandler(HttpServletRequest request,
			HttpServletResponse response) {
		this.request = request;
		this.response = response;

		this.key = "";
		this.parameters = new TreeMap();
		this.debugInfo = "";

		this.uriEncoding = "";

		String inputLine;
		String notityXml = "";
		try {
			while ((inputLine = this.request.getReader().readLine()) != null) {
				notityXml += inputLine;
			}
			request.getReader().close();
		} catch (Exception e) {
			logger.error("xml获取失败：" + e);

		}
		logger.info("接收到的报文：" + notityXml);

		Map m = null;
		try {
			m = XMLUtil.doXMLParse(notityXml);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			logger.error("ResponseHandler" ,e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("ResponseHandler" ,e);
		}
		if (null != m) {
			Iterator it = m.keySet().iterator();
			logger.info("m：" + m.toString());
			while (it.hasNext()) {
				String k = (String) it.next();
				String v = ((String) m.get(k));
				this.setParameter(k, v);
			}
		}
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getParameter(String parameter) {
		String s = (String) this.parameters.get(parameter);
		return (null == s) ? "" : s;
	}
	public void setParameter(String parameter, String parameterValue) {
		String v = "";
		if (null != parameterValue) {
			v = parameterValue.trim();
		}
		this.parameters.put(parameter, v);
	}
	public SortedMap getAllParameters() {
		return this.parameters;
	}
	public boolean isTenpaySign() {
		logger.debug("isTenpaySign()");
		StringBuffer sb = new StringBuffer();
		Set es = this.parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if (!"sign".equals(k) && null != v && !"".equals(v)) {
				sb.append(k + "=" + v + "&");
			}
		}

		sb.append("key=" + this.getKey());
		logger.info(sb);
		// 算出摘要
		String enc = TenpayUtil.getCharacterEncoding(this.request,
				this.response);
		String sign = MD5.MD5Encode(sb.toString(), enc).toLowerCase();
		String tenpaySign = this.getParameter("sign").toLowerCase();
		// debug信息
		this.setDebugInfo(sb.toString() + " => sign:" + sign + " tenpaySign:"
				+ tenpaySign);

		return tenpaySign.equals(sign);
	}
	public void sendToCFT(String msg) throws IOException {
		String strHtml = msg;
		PrintWriter out = this.getHttpServletResponse().getWriter();
		out.println(strHtml);
		out.flush();
		out.close();

	}
	public String getUriEncoding() {
		return uriEncoding;
	}
	public void setUriEncoding(String uriEncoding)
			throws UnsupportedEncodingException {
		if (!"".equals(uriEncoding.trim())) {
			this.uriEncoding = uriEncoding;

			// 编码转换
			String enc = TenpayUtil.getCharacterEncoding(request, response);
			Iterator it = this.parameters.keySet().iterator();
			while (it.hasNext()) {
				String k = (String) it.next();
				String v = this.getParameter(k);
				v = new String(v.getBytes(uriEncoding.trim()), enc);
				this.setParameter(k, v);
			}
		}
	}
	public String getDebugInfo() {
		return debugInfo;
	}
	protected void setDebugInfo(String debugInfo) {
		this.debugInfo = debugInfo;
	}
	protected HttpServletRequest getHttpServletRequest() {
		return this.request;
	}
	protected HttpServletResponse getHttpServletResponse() {
		return this.response;
	}

}